package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.LogService;
import com.example.demo.service.UserService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
public class UserController2 {
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;

    @RequestMapping("/save")
    public Object save(@RequestBody User user) {
        user.getUserId();
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("message","当前错误");
        return hashMap;
    }
    // JDBC 事务管理器
//    @Resource
//    private DataSourceTransactionManager dataSourceTransactionManager;
//    // 定义事务属性
//    @Resource
//    private TransactionDefinition transactionDefinition;
//
//    @RequestMapping("/save")
//    public Object save(User user) {
//        // 1. 开启事务
//        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
//        // 插入数据库
//        int result = userService.save(user);
//        // 2. 提交事务
//        dataSourceTransactionManager.commit(transactionStatus);
//        // 3. 回滚事务
//        dataSourceTransactionManager.rollback(transactionStatus);
//        return result;
//    }
}
